/**
 * Created by Administrator on 2017/3/7.
 */

var pomelo = require('pomelo');
var md5 = require("./md5");
var config = require("./wszf")
var systemConfig = require('../../../config/systemConfig.json');
module.exports = {
    pay: function (query, callback) {
        var uid = 0;
        if (query.hasOwnProperty('uid')) {
            if (!isNaN(query.uid)) {
                uid = parseInt(query.uid);
            }
        } else if (query.openid) {
            var uid = gPlayerManger.getUid(query.openid);
        }

        if (!uid) {
            callback(null, 2);
        }

        gPlayerManger.getPlayer(uid, function (player) {
            if (!player) {
                callback(null, 2); return;
            }

            var num = parseInt(query.card || query.cash);
            player.payCard(num);

            DB.pay.save({
                agent: 0,
                uid: uid,
                num: num,
                t: Date.getStamp()
            });

            callback(null, 1);
        });
    },
    wsfpay: function (query, callback) {

        var usercode = query["p1_usercode"]
        var signMsg = systemConfig["wszf"][usercode]["signMsg"];

        var md5info_param = "";
        md5info_param = md5info_param.concat(query.p1_usercode, "&", query.p2_order, "&", query.p3_money, "&", query.p4_status,
            "&", query.p5_payorder, "&", query.p6_paymethod, "&", query.p7_paychannelnum, "&", query.p8_charset, "&", query.p9_signtype, "&", signMsg);
        var md5info_item = md5(md5info_param);
        md5info_item = md5info_item.toUpperCase();
        if (query.p10_sign == md5info_item) {
            if (query.p11_remark == '') {
                //console.log('remark empty')
                return;
            }
            //console.log(query.p10_sign == md5info_item)
            var uid = 0;
            var sendObj;
            if (query.hasOwnProperty('p11_remark')) {
                //console.log(query.p11_remark);
                sendObj = JSON.parse(query.p11_remark)
                //console.log("sendObj")
                //console.log(sendObj);
                if (!isNaN(sendObj.uid)) {
                    uid = parseInt(sendObj.uid);
                }
            }
            //console.log('uid')
            //console.log(uid)
            if (!uid) {
                callback(null, 2);
                return;
            }

            var findCondition = {
                "_id": 'wszf' + query.p2_order
            }

            var refer = 0;
            gPlayerManger.getPlayer(uid, function (player) {
                if (!player) {
                    return;
                }
                if (player.getInviterUid()) {
                    refer = player.getInviterUid();
                }
            });

            async.waterfall([
                function (cb) {
                    DB.onlinepay.findOne(findCondition, function (err, result) {
                        if (err) {
                            cb(err)
                            return
                        }
                        if (!result) {
                            if (query.p4_status == 1) {
                                gPlayerManger.getPlayer(uid, function (player) {
                                    if (!player) {
                                        //console.log('donthave')
                                        callback(null, 2); return;
                                    }
                                    var value = systemConfig["wszf"][query.p1_usercode].foodId[sendObj.goods[0].id];
                                    var cardsNum = value;
                                    //console.log(cardsNum);
                                    callback(null, 1);
                                    var reason = "buyCards";
                                    player.addResource("card", cardsNum, reason);
                                    var time = Date.parse(new Date());
                                    player.feedbackAgency({ "money": query.p3_money, "time": time, "order": findCondition._id })
                                    var sets = {
                                        "_id": findCondition._id,
                                        "platform": "wszf",
                                        "status": 1,
                                        "bank": "",
                                        "uid": uid,
                                        "money": query.p3_money,
                                        "item": sendObj.goods[0].id,
                                        "time": time,
                                        "refer": refer
                                    };
                                    //console.log(sets);
                                    DB.onlinepay.save(sets, function (err) {
                                        //console.log("save errr");
                                    });
                                });
                            } else {
                                var sets = {
                                    "_id": findCondition._id,
                                    "platform": "wszf",
                                    "status": 0,
                                    "bank": "",
                                    "uid": uid,
                                    "money": query.p3_money,
                                    "item": sendObj.goods[0].id,
                                    "time": Date.parse(new Date()),
                                    "refer": refer
                                };
                                //console.log(sets);
                                DB.onlinepay.save(sets, function (err) {
                                    //console.log("save errr");
                                });
                            }
                            return;
                        } else {
                            cb(null, result)
                        }
                    });
                },
                function (result, cb) {
                    if (!result.status) {
                        if (query.p4_status == 1) {
                            gPlayerManger.getPlayer(uid, function (player) {
                                if (!player) {
                                    callback(null, 2); return;
                                }
                                // var value = parseFloat(query.p3_money);
                                // console.log(value)
                                // var rate = config.rate;
                                // var cardsNum = rate[value];
                                var value = systemConfig["wszf"][query.p1_usercode].foodId[sendObj.goods[0].id];
                                var cardsNum = value;

                                callback(null, 1);
                                var reason = "buyCards";
                                player.addResource("card", cardsNum, reason);
                                player.feedbackAgency({ "money": query.p3_money, "time": Date.parse(new Date()), "order": findCondition._id })
                                var sets = {
                                    "_id": findCondition._id,
                                    "platform": "wszf",
                                    "status": 1,
                                    "bank": "",
                                    "uid": uid,
                                    "money": query.p3_money,
                                    "item": sendObj.goods[0].id,
                                };
                                DB.onlinepay.update({ "_id": findCondition._id }, { '$set': sets }, function (err) {
                                    //console.log("save errr");
                                });
                            });
                        }
                        logger.info("also cant pay");
                    }
                    logger.info("already payed");
                },
            ], function (err) {
                logger.err("DB err")
            }
            );
            callback();
            return;
        }
        callback();
    }
}